package swd.maintain.project.user_group_maintain;

import com.pub.database.*;
import java.util.*;

public class Business {
    QueryBean gqbnDAO = null;
    public Business(QueryBean pqbnDAO) {
        gqbnDAO = pqbnDAO;
    }

    String selectTableDataToFld(View view) throws Exception {
        String lstrSQL = null;
        Hashtable<String,Object> lhtTableData = null;
        try {
            lstrSQL = "select user_no,group_no from wkf_group where group_no='" +
                      view.getgroup_no() + "'" + " and user_no='" +
                      view.getuser_no() + "'";
            lhtTableData = gqbnDAO.readSingleRowData(lstrSQL);
            if (lhtTableData == null) {
                return "查無資料";
            }
            view.setuser_no(String.valueOf((lhtTableData.get("USER_NO").
                                            toString())));
            view.setgroup_no(String.valueOf((lhtTableData.get("GROUP_NO").
                                             toString())));

            return "";
        } catch (Exception e) {
            throw new Exception(e.toString());
        } finally {
            lhtTableData.clear();
            lhtTableData = null;
        }
    }

    String exeUpdSQL(View view) throws Exception {
        String lstrSQL = new String();
        Vector<Object> lvtrParam = new Vector<Object>();
        try {
            lvtrParam.add(view.getuser_no() == null ? null :
                          String.valueOf(view.getuser_no()));
            lvtrParam.add(view.getgroup_no() == null ? null :
                          String.valueOf(view.getgroup_no()));
            lstrSQL =
                    "update wkf_group set user_no=?,group_no=? where group_no='" +
                    view.getgroup_no() + "'" + " and user_no='" +
                    view.getuser_no() + "'";
            if (!gqbnDAO.executeSQL(lstrSQL, lvtrParam)) {
                return "wkf_group";
            }
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.toString());
        } finally {
            lvtrParam.clear();
        }
    }

    String exeInsSQL(View view) throws Exception {
        String lstrSQL = new String();
        Vector<Object> lvtrParam = new Vector<Object>();
        try {
            lvtrParam.add((view.getgroup_no() == null ? null : view.getgroup_no()));
            lvtrParam.add((view.getuser_no() == null ? null : view.getuser_no()));
            lstrSQL = "insert into wkf_group(group_no,user_no) values (?,?)";
            if (!gqbnDAO.executeSQL(lstrSQL, lvtrParam)) {
                ;
                return "wkf_group";
            }
            lvtrParam.clear();

            return "";
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        } finally {
            lvtrParam.clear();
        }
    }

    void exeDelSQL(View view, int pintSn) {
        String lstrSQL = new String();
        try {
            lstrSQL = "delete from wkf_group where group_no='" +
                      view.getgroup_no_g()[pintSn].trim() + "' and user_no='" +
                      view.getuser_no_g()[pintSn] + "' ";
            gqbnDAO.executeSQL(lstrSQL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    void setDataViewForm(DataViewForm DataViewForm, View view,
                         String pstrLoginUser) throws Exception {

        //StringBuffer stbCondition = new StringBuffer();
        StringBuffer stbPageActionURI = new StringBuffer(
                "user_group_maintain.jsp?rowsn=");

        DataViewForm.setFieldsList(
                " '',a.user_no,b.employee_name_c,b.employee_name_e,a.group_no");
        DataViewForm.setTablesList(" wkf_group as a left outer join pnl_employee as b on a.user_no=b.employee_no ");
        DataViewForm.setSQLCondition("a.group_no = '" + view.getgroup_no() +
                                     "' ");
        DataViewForm.setOrderByFields("a.user_no");
        DataViewForm.setOrderBySeq("asc");
        DataViewForm.setGroupBy(null);
        DataViewForm.setHaving(null);

        String strColRelation[][] = new String[5][];

        strColRelation[0] = new String[5];
        strColRelation[1] = new String[5];
        strColRelation[2] = new String[5];
        strColRelation[3] = new String[5];
        strColRelation[4] = new String[5];

        strColRelation[0][0] = "''";
        strColRelation[0][1] = "INPUT";
        strColRelation[0][2] = "WIDTH='10%' align='center'";
        strColRelation[0][3] = "<INPUT class='button b_normal' TYPE=\"BUTTON\" onclick=\"javascript:saveRowSn('%sn%');document.all.item('prgAction').value='delete';document.forms[0].submit();\" NAME=\"del\" VALUE=";
        strColRelation[0][4] = "'刪除'";

        strColRelation[1][0] = "'工號'";
        strColRelation[1][1] = "INPUT";
        strColRelation[1][2] = "WIDTH='15%'";
        strColRelation[1][3] = "<INPUT TYPE='text' size='8' name='user_no_g' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[1][4] = "1";

        strColRelation[2][0] = "'中文姓名'";
        strColRelation[2][1] = "INPUT";
        strColRelation[2][2] = "WIDTH='30%'";
        strColRelation[2][3] = "<INPUT TYPE='text' size='8' name='name_c' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[2][4] = "2";

        strColRelation[3][0] = "'英文姓名'";
        strColRelation[3][1] = "INPUT";
        strColRelation[3][2] = "WIDTH='45%'";
        strColRelation[3][3] = "<INPUT TYPE='text' size='8' name='name_e' readonly CLASS='input_noborder' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
        strColRelation[3][4] = "3";

        strColRelation[4][0] = "HIDDEN";
        strColRelation[4][1] = "INPUT";
        strColRelation[4][2] = "WIDTH='0%'";
        strColRelation[4][3] =
                "<INPUT TYPE='HIDDEN' size='8' name='group_no_g' VALUE=";
        strColRelation[4][4] = "4";

        DataViewForm.setColRelation(strColRelation); //將欄位相關設定殖入ActionForm
        DataViewForm.setJumpPageButtonShow(false); //是否要秀出上下頁按鈕
        DataViewForm.setPageBtnOnTop(false); //將button and rows info 秀在頂端
        DataViewForm.setPageBtnOnButtom(true); //將button and rows info 秀在底端
        DataViewForm.setFilterShow(false); //是否要秀出過濾選擇欄位
        DataViewForm.setJumpPageShow(false); //是否要秀出跳頁下拉式選單
        DataViewForm.setRowsInfoShow(false); //是否要秀出筆數資訊
        DataViewForm.setRowPageShow(false); //是否要秀出每頁筆數控制欄
        DataViewForm.setExcelShow(false); //是否要秀出轉Excel button
        DataViewForm.setOrderEnable(false); //是否要提供排序點選功能
        DataViewForm.setShowHeaderWhenNoData(false); //當ResultSet無資料是否要秀Header

        DataViewForm.setTableHeaderHtml(
                "class='td_head_grid' align='center'  width='680px'");
        //設定上一頁與下一頁按鈕的form action(通常為母頁即本頁)
        DataViewForm.setPageActionURI(stbPageActionURI.toString());
    }

    String checkHaveEmpno(View view) throws Exception {
        String strReturn = "0";
        strReturn = gqbnDAO.readSingleValueData(
                "select count(employee_no) from pnl_employee where employee_no='" +
                view.getuser_no() + "'");
        return strReturn;
    }

    String checkExitEmpno(View view) throws Exception {
        String strReturn = "0";
        strReturn = gqbnDAO.readSingleValueData(
                "select count(*) from wkf_group where group_no='" +
                view.getgroup_no().trim() + "' and user_no='"+view.getuser_no().trim()+"'");
        return strReturn;
    }


    String checkNotNullFld(View view) throws Exception {
        StringBuffer lstbNullFldList = new StringBuffer();
        lstbNullFldList.append(checkHaveEmpno(view).equals("0") ?
                               "沒有這個工號!" + "," : "");
        lstbNullFldList.append(!checkExitEmpno(view).equals("0") ?
                               "工號已存在" + "," : "");
        return (lstbNullFldList.toString().equals("") ? "" :
                lstbNullFldList.
                delete(lstbNullFldList.length() - 1, lstbNullFldList.length()).
                toString());
    }

    void setInitValueForAdd(View view, String pstrUserID) throws Exception {
    }

    void setReadonlyForAdd(View view) throws Exception {
        view.setgroup_no_readonly(true);
        view.setuser_no_readonly(false);
    }

}
